import Vue from 'vue';
import VueRouter from 'vue-router';
import routes from './route';
import  { getAdmin } from '@A/resource/admin';

Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
};


const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
});
router.beforeEach((to, from, next) => {
  let isLogin = getAdmin(localStorage, 'userInfo'); //  判断是否登录，本地存储有用户数据则视为已经登录
  if (isLogin) {
    if (to.path === '/') {
      next({path: '/user-info'});
    } else {
      next();
    }
  } else {
    if (to.path === '/') {
      next();
    } else {
      next({path: '/'});
    }
  }
});



export default router
